﻿//=======================================================
// Author: Jonas Follesø <jonas@follesoe.no>
// Copyright (c) 2008
//
// Licensed under the Microsoft Public License (Ms-PL)
// A copy of the license is available at 
// http://www.opensource.org/licenses/ms-pl.html
//=======================================================

using System;
using System.Windows.Data;
using System.Globalization;

namespace Colorful.Control.Converters
{
    internal class MaxStringLengthConverter : IValueConverter
    {
        public bool AddEllipsis { get; set; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string input = (string)value;
            int maxLength = System.Convert.ToInt32(parameter);

            if (input.Length > maxLength)
            {
                if (AddEllipsis) return input.Substring(0, maxLength) + "...";
                else return input.Substring(0, maxLength);
            }
            else return input;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}